<?php
namespace app\libs;

class Util {
    public static function plainText($str) {
        if(!$str) {
            return '';
        }

        $ret = str_replace("'", '', $str);
        $ret = str_replace('"', '', $ret);
        $ret = strip_tags($ret);

        return $ret;
    }

    public static function strlenOfCN($str) {
        return mb_strlen($str, 'UTF-8');
    }

    public function substrOfCN($str, $start, $len) {
        return mb_substr($str, $start, $len, 'UTF-8');
    }

    // U('posts', ['id' => 1]) -> /posts/1 或 /index.php?r=posts&id=1
    public static function U($url, $params = [], $subfix = '') {
        $query = '';
        $prettyRouter = \Fate::$app->iniReader->get('system', 'pretty_router');

        if('1' === $prettyRouter) {
            $firstParam = '';
            foreach($params as $key => $val) {
                $firstParam = '/' . $val;
                break;
            }
            $url = '/' . $url . $firstParam . $subfix;

        } else {
            $url = '/index.php?r=' . $url;
            // id=1&age=20&
            foreach($params as $key => $val) {
                $query = $query . $key . '=' . $val . '&';
            }

            if('' !== $query) {
                $url .= '&' . $query;

                $url = rtrim($url, '&');
            }
        }

        return \fate\http\Request::getInstance()->getHostInfo() . $url;
    }

    public static function shortNumber($n) {
        if($n < 1000) {
            return $n;
        }

        if($n >= 1000 && $n < 10000) {
            return sprintf('%.1f', $n / 1000) . 'k';
        }

        if($n >= 10000 && $n < 10000000) {
            return sprintf('%.1f', $n / 10000) . 'w';
        }

        if($n >= 10000000 && $n < 100000000) {
            return sprintf('%.1f', $n / 10000000) . 'kw';
        }

        return sprintf('%.1f', $n / 100000000) . 'hm';
    }

    public static function listToTree($list, $pk='id', $pid = 'pid', $child = '_child', $root='0') {
        // 创建 Tree
        $tree = [];
        if(is_array($list)) {
            // 创建基于主键的数组引用
            $refer = [];
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] = & $list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在 parent
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[] = & $list[$key];
                }else{
                    if (isset($refer[$parentId])) {
                        $parent = & $refer[$parentId];
                        $parent[$child][] = & $list[$key];
                    }
                }
            }
        }

        return $tree;
    }

    public static function defaultAvatar($src) {
        return empty($src) ? RES . '/pc/images/avatar.jpg' : $src;
    }

    public static function parseArticleTags($tags) {
        if('' === $tags) {
            return '';
        }

        $arr = explode(',', $tags);
        $str = '';

        foreach($arr as $val) {
            $str .= '<a class="x-tag" target="_blank" href="'. self::U('tag', ['k' => $val]) .'">'. $val .'</a>';
        }

        return $str;
    }

    // 检查生日是否满足文章分级限制
    public static function checkArticleRating($rating, $birthday) {
        $ratingInt = intval($rating);
        $birthdayInt = intval($birthday);

        // suppose 365 days of a year = 60 * 60 * 24 * 365 seconds
        $yearTime = 31536000;
        $now = time();
        $legal = false;

        switch($ratingInt) {
            case 1:
                $legal = true;
                break;
            case 2:
                // need age of 6
                if($birthdayInt + $yearTime * 6 > $now) {
                    $legal = true;
                }
                break;
            case 3:
                // need age of 12
                if($birthdayInt + $yearTime * 12 > $now) {
                    $legal = true;
                }
                break;
            case 4:
                // need age of 15
                if($birthdayInt + $yearTime * 15 > $now) {
                    $legal = true;
                }
                break;
            case 5:
                // need age of 18
                if($birthdayInt + $yearTime * 18 > $now) {
                    $legal = true;
                }
                break;
            default:
                break;
        }

        return $legal;
    }
}
